//
//  HomeADRequest.m
//  BaseProject
//
//  Created by 王洋 on 2021/3/9.
//  Copyright © 2021 王洋. All rights reserved.
//

#import "HomeADRequest.h"

@implementation HomeADRequest

+ (void)request_homeMainWithCityName:(NSString *)cityName cityId:(NSString *)cityId comption:(void (^)(BOOL success, HomeModel *homeModel))comption {
    
    HomeModel *homeModel = [[HomeModel alloc] init];

    NSMutableDictionary *dic = @{}.mutableCopy;
    [dic setValue:[TTUserModel sharedObject].cityName forKey:@"cityName"];
    [dic setValue:[TTUserModel sharedObject].cityId forKey:@"sellerCityId"];

    NSString *url = [NSString stringWithFormat:@"%@%@",TT_Global_Api_Domain,API_Home];
    
    [TTHttpManager postRequestWithUrl:url TimeOut:10 PostData:dic FinishBlock:^(id  _Nonnull data) {

        if ([data isKindOfClass:[NSDictionary class]]) {
            //请求成功
            NSInteger code = [[data valueForKey:@"code"] integerValue];
            if (code == 10000) {
                NSDictionary *resultDic = [data valueForKey:@"data"];
                if (![resultDic isKindOfClass:[NSDictionary class]]) {
                    return;
                }
                NSDictionary *sysAdvTemplate = [resultDic valueForKey:@"sysAdvTemplate"];
                NSDictionary *head = [resultDic valueForKey:@"head"];
                NSDictionary *startpage = [resultDic valueForKey:@"startpage"];
                NSArray *searchWordsEventList = [resultDic valueForKey:@"searchWordsEventList"];

                
                [[TTUserModel sharedObject].userModelDefault setValue:startpage forKey:@"BA_startPage"];
                [[TTUserModel sharedObject].userModelDefault setValue:searchWordsEventList forKey:@"BA_searchWordsEventList"];

                NSString *startPageUrl = [startpage valueForKey:@"advBackImg"];
                [TT_OSSManager cacheSourceUrl:startPageUrl];
                
                [[TTUserModel sharedObject].userModelDefault synchronize];
                
                NSArray *sysAdvEvent = [resultDic valueForKey:@"sysAdvEvent"];
                
                homeModel.freeNewProArray = @[].mutableCopy;
                
                if ([head isKindOfClass:[NSDictionary class]]) {
                    homeModel.head = [HomeHeadModel modelWithDictionary:head];
                    NSArray *sysAdvEventList = [head valueForKey:@"sysAdvEventList"];
                    if ([sysAdvEventList isKindOfClass:[NSArray class]]) {
                        homeModel.head.sysAdvEventList = @[].mutableCopy;

                        if (sysAdvEventList.count > 0) {
                            for (int j = 0; j < sysAdvEventList.count; j++) {
                                NSDictionary *sysAdvEventListDic = sysAdvEventList[j];
                                HomeADItemEventModel *eventModel = [HomeADItemEventModel modelWithDictionary:sysAdvEventListDic];
                                
                                [homeModel.head.sysAdvEventList addObject:eventModel];
                            }
                        }
                    } else {
                        homeModel.head.sysAdvEventList = @[].mutableCopy;
                        HomeADItemEventModel *eventModel = [[HomeADItemEventModel alloc] init];
                        eventModel.eventClickId = TT_JUMPTYPE_Search;
                        eventModel.iconName = @"“TA说”快速搞定备婚难题";
                        [homeModel.head.sysAdvEventList addObject:eventModel];
                    }
                } else {
                    homeModel.head = [[HomeHeadModel alloc] init];
                    homeModel.head.sysAdvEventList = @[].mutableCopy;
                    HomeADItemEventModel *eventModel = [[HomeADItemEventModel alloc] init];
                    eventModel.eventClickId = TT_JUMPTYPE_Search;
                    eventModel.iconName = @"“TA说”快速搞定备婚难题";
                    [homeModel.head.sysAdvEventList addObject:eventModel];
                }
                
                if (![sysAdvTemplate isKindOfClass:[NSDictionary class]]) {
                    homeModel.adDataArray = @[].mutableCopy;

                    comption(NO,homeModel);
                    return;
                }
                
                if ([sysAdvEvent isKindOfClass:[NSArray class]]) {
                    homeModel.sysAdvEvent = @[].mutableCopy;
                    for (int i = 0; i < sysAdvEvent.count; i++) {
                        HomeADItemEventModel *advModel = [HomeADItemEventModel modelWithDictionary:sysAdvEvent[i]];
                        [homeModel.sysAdvEvent addObject:advModel];
                    }
                }
                
                NSArray *sellerGoodsList = [resultDic valueForKey:@"sellerGoodsList"];
                if ([sellerGoodsList isKindOfClass:[NSArray class]]) {
                    homeModel.freeNewProArray = @[].mutableCopy;
                    for (int i = 0; i < sellerGoodsList.count; i++) {
                        CommonPackageModel *proModel = [CommonPackageModel modelWithJSON:sellerGoodsList[i]];
                        
                        NSString *url = [TT_OSSManager reSourceUrl:proModel.fangUrl type:BA_resizeType_plus];
                        proModel.fangUrl = url;
                        [homeModel.freeNewProArray addObject:proModel];
                    }
                }
                
                NSArray *sysAdvMoudleList = [sysAdvTemplate valueForKey:@"sysAdvMoudleList"];
                if (![sysAdvMoudleList isKindOfClass:[NSArray class]]) {
                    sysAdvMoudleList = @[];
                }
                
                homeModel.scrollStopHeight = CGFLOAT_MIN;
                [homeModel.adDataArray removeAllObjects];
                for (int i = 0; i < sysAdvMoudleList.count; i++) {
                    NSDictionary *sysAdvMoudleListDic = sysAdvMoudleList[i];
                    HomeADItemModel *adItemModel = [HomeADItemModel modelWithDictionary:sysAdvMoudleListDic];
                    NSDictionary *sysAdvertisement = [sysAdvMoudleListDic valueForKey:@"sysAdvertisement"];
                    // 过滤掉icons
                    if (adItemModel.tadmId == HomeFlow_itemType_icon) {
                        //break;
                    }
                    switch (adItemModel.tadmId) {
                        case HomeFlow_itemType_bigBanner:{
                            adItemModel.itemHeight = (TTScreenWidth-20)*0.367 + 20;
                        } break;
                        case HomeFlow_itemType_gird:{
                            adItemModel.itemHeight = [HomeFlowServiceCatCell calculateHeight];
                        } break;
                        case HomeFlow_itemType_mixGird:{
                            adItemModel.itemHeight = [HomeFlowServiceCatCell_style2 calculateHeight];
                        } break;
                        case HomeFlow_itemType_icon:{
                            adItemModel.itemHeight = [HomeFlowStoreCatCell calculateHeight:10];
                        } break;
                        case HomeFlow_itemType_mixIcon:{
                            adItemModel.itemHeight = [HomeFlowStoreCatCell_style2 calculateHeight];
                        } break;
                        case HomeFlow_itemType_title:{
                            adItemModel.itemHeight = TTScreenWidth * 0.093;
                        } break;
                        case HomeFlow_itemType_YXStore:{
                            adItemModel.itemHeight = [HomeFlowStrictElectionStoreCell calculateHeight];
                        } break;
                        case HomeFlow_itemType_xBanner:{
                            adItemModel.itemHeight = (TTScreenWidth-20)*0.212 + 20;
                        } break;
                        case HomeFlow_itemType_newGird:{
                            adItemModel.itemHeight = [HomeFlowBannerTopicCell calculateHeight];
                        } break;
                        case HomeFlow_itemType_newFree:{
                            switch ([TTUserModel sharedObject].isNewer) {
                                case TTUserType_unKnow:{
                                    adItemModel.itemHeight = CGFLOAT_MIN;
                                } break;
                                case TTUserType_new:{
                                    adItemModel.itemHeight = CGFLOAT_MIN;
                                } break;
                                case TTUserType_old:{
                                    adItemModel.itemHeight = CGFLOAT_MIN;
                                } break;
                                default:
                                    break;
                            }
                        } break;
                        default: {
                            adItemModel.itemHeight = CGFLOAT_MIN;
                        }
                            break;
                    }
                    homeModel.scrollStopHeight = homeModel.scrollStopHeight + adItemModel.itemHeight;
                    adItemModel.photos = @[].mutableCopy;
                    if ([sysAdvertisement isKindOfClass:[NSDictionary class]]) {
                        adItemModel.sysAdvertisement = [HomeADItemInfoModel modelWithDictionary:sysAdvertisement];

                        NSArray *sysAdvEventList = [sysAdvertisement valueForKey:@"sysAdvEventList"];
                        if ([sysAdvEventList isKindOfClass:[NSArray class]]) {
                            adItemModel.sysAdvertisement.sysAdvEventList = @[].mutableCopy;

                            for (int j = 0; j < sysAdvEventList.count; j++) {
                                NSDictionary *sysAdvEventListDic = sysAdvEventList[j];
                                HomeADItemEventModel *eventModel = [HomeADItemEventModel modelWithDictionary:sysAdvEventListDic];
                                // 过滤掉单个津贴 王盼盼
                                if (eventModel.eventClickId != 19) {
                                    
                                }
                                [adItemModel.photos addObject:eventModel.url];
                                [adItemModel.sysAdvertisement.sysAdvEventList addObject:eventModel];
                                // mmd
                                //NSLog(@"mmd %ld %@", eventModel.eventClickId, eventModel.eventClickName);
                            }
                        }
                        
                        NSArray *excList = [sysAdvertisement valueForKey:@"excList"];
                        if ([excList isKindOfClass:[NSArray class]]) {
                            adItemModel.sysAdvertisement.excList = @[].mutableCopy;

                            for (int j = 0; j < excList.count; j++) {
                                NSDictionary *excDic = excList[j];
                                HomeADItemEventModel *eventModel = [HomeADItemEventModel modelWithDictionary:excDic];
                                eventModel.index = j;
                                [adItemModel.sysAdvertisement.excList addObject:eventModel];
                            }
                        }
                    }
                    [homeModel.adDataArray addObject:adItemModel];
                    // 自定义高度
                    if (adItemModel.tadmId == HomeFlow_itemType_icon) {
                        adItemModel.itemHeight = [HomeFlowStoreCatCell calculateHeight:adItemModel.sysAdvertisement.sysAdvEventList.count];
                    }
                }
                comption(YES,homeModel);
            } else {
                comption(NO,homeModel);
            }
        }

    } FailedBlock:^(NSError * _Nonnull error) {
        comption(NO,nil);
    }];
}

@end
